home *** CD-ROM | disk | FTP | other *** search
/ Aminet 21 / Aminet 21 (1997)(GTI - Schatztruhe)[!][Oct 1997].iso / Aminet / comm / bbs / cit_src_7H21.lha / netstat.c < prev    next >
C/C++ Source or Header  |  1997-07-27  |  7KB  |  253 lines

  1. /**
  2.   Network Statistics Version 1.02:
  3.  
  4.   Input:
  5.       Datafile created by Citadel
  6.       cc       1    1     2    2 2 3      3 34       4 5        5
  7.       1   5 7  0    5     0    6 8 0      7 90       8 0        9
  8.       MMMYY xxxxxxxxxxxxxxxxxxxx nnnnnnnnnn nnnnnnnnnn nnnnnnnnnn
  9.             Name of Net Partner  chars in   chars out    time
  10.  
  11.   Name of datafile is 1st argument.
  12.   Optional arguments:
  13.    flag
  14.      -S    to report by system.
  15.      -M    to report totals on a monthly basis.
  16.  
  17.   Output:
  18.       Report
  19.  
  20. **/
  21. #include <stdio.h>
  22. #include <stdlib.h>
  23. #include <string.h>
  24. #include <dos.h>
  25. #include <math.h>
  26. #include "sysdep.h"
  27.  
  28. struct bbs
  29.   {
  30.   struct bbs *next;     /* next bbs system */
  31.   char name[21];        /* name of bbs */
  32.   long net_count[12];   /* number of network sessions */
  33.   long chars_in[12];    /* monthly totals of characters input */
  34.   long chars_out[12];   /* monthly totals of characters output */
  35.   long time_used[12];   /* monthly totals of time used in networking */
  36.   long min_cps[12];         /* minimum cps that occured */
  37.   long max_cps[12];         /* maximum cps that occured */
  38.   };
  39.  
  40. struct bbs *systems = NULL; /* list of systems */
  41. int sflag = FALSE;      /* no individual per system statistics */
  42. int mflag = FALSE;      /* no monthy statistics */
  43. int lineno= 0;          /* current line number in the file */
  44. void Collect_Statistics(FILE *ip);
  45. void Report_Statistics(void);
  46. int main(int argc, char *argv[]);
  47. struct bbs *Find_System(char *name);
  48. void Report_BBS(struct bbs *ptr);
  49. int Date_to_Index(char *date);
  50.  
  51.  
  52. int main(int argc, char *argv[])
  53.   {
  54.   FILE *ip;
  55.   char *name=NULL;
  56.   printf("   Citadel Networking Statistics %s\n", VERSION_NAME);
  57.   while( --argc )
  58.     {
  59.     if( argv[argc][0] == '-')
  60.       {
  61.       if( argv[argc][1] == 'S') sflag = TRUE;
  62.       if( argv[argc][1] == 's') sflag = TRUE;
  63.       if( argv[argc][1] == 'M') mflag = TRUE;
  64.       if( argv[argc][1] == 'm') mflag = TRUE;
  65.       }
  66.      else name = argv[argc];
  67.      };
  68.   if( name == NULL)
  69.     {
  70.     fprintf(stderr,"Error, need a file to process\n");
  71.     return(100);
  72.     };
  73.   if( (ip=fopen(name,"r")) == NULL )
  74.     {
  75.     fprintf(stderr," Error opening %s for input\n",argv[argc]);
  76.     return(100);
  77.     };
  78.   Collect_Statistics(ip);
  79.   fclose(ip);
  80.   Report_Statistics();
  81.   return 0;
  82.   }
  83.  
  84. void Collect_Statistics(FILE *ip)
  85.   {
  86.   struct bbs *ptr;
  87.   char line[80];
  88.   int did;
  89.   while( fgets(line, sizeof(line), ip) )
  90.     {
  91.     long  nci, nco, ntm;
  92.     lineno++;
  93.     line[ 3] = '\0';
  94.     line[27] = '\0';
  95.     line[38] = '\0';
  96.     line[49] = '\0';
  97.     line[60] = '\0';
  98.     did  = Date_to_Index(&line[ 0]);
  99.     ptr = Find_System(&line[ 7]);
  100.     ptr->net_count[did]++;
  101.     ptr->chars_in[did]  += (nci = atol(&line[28]));
  102.     ptr->chars_out[did] += (nco = atol(&line[39]));
  103.     ptr->time_used[did] += (ntm = atol(&line[50]));
  104.     nci += nco;
  105.     if( ntm > 0 )
  106.       {
  107.       nco = nci / ntm;
  108.       if( nco < ptr->min_cps[did]) ptr->min_cps[did] = nco;
  109.       if( nco > ptr->max_cps[did]) ptr->max_cps[did] = nco;
  110.       };
  111.     };
  112.   }
  113.  
  114. char *month[12] =
  115.   {
  116.   "jan", "feb", "mar", "apr", "may", "jun",
  117.   "jul", "aug", "sep", "oct", "nov", "dec"
  118.   };
  119.  
  120.  
  121. int Date_to_Index(char *date)
  122.   {
  123.   int i;
  124.   if( ! mflag )return 0;  /* if not doing monthly stats, skip this */
  125.   for( i=0; i<12; i++)
  126.     {
  127.     if( stricmp(month[i],date)== 0 )return i;
  128.     };
  129.   fprintf(stderr,"Error: INVALID DATE at line %d\n",lineno);
  130.   return 0;
  131.   }
  132.  
  133. struct bbs *Find_System(char *name)
  134.   {
  135.   struct bbs *ptr=systems;
  136.   int i;
  137.   if( sflag )
  138.     {
  139.     while ( ptr != NULL )
  140.       {
  141.       if( strcmp(ptr->name,name) == 0)return ptr;
  142.       ptr = ptr->next;
  143.       };
  144.     if( ptr == NULL )
  145.       {
  146.       ptr = calloc(1, sizeof(struct bbs));
  147.       strcpy(ptr->name,name);
  148.       for(i=0; i<12; i++)
  149.         {
  150.         ptr->min_cps[i] = 99999;
  151.         ptr->max_cps[i] = -1;
  152.         };
  153.       ptr->next    = systems;
  154.       systems      = ptr;
  155.       };
  156.     }
  157.   else
  158.     {
  159.     if( systems == NULL )
  160.       {
  161.       ptr = systems = calloc(1, sizeof(struct bbs));
  162.       strcpy(ptr->name," *** All Systems ***");
  163.       for(i=0; i<12; i++)
  164.         {
  165.         ptr->min_cps[i] = 99999;
  166.         ptr->max_cps[i] = -1;
  167.         };
  168.       };
  169.     };
  170.   return ptr;
  171.   }
  172.  
  173. void Report_Statistics(void)
  174.   {
  175.   long hr, sec, mins, avr_cps;
  176.   struct bbs *ptr=systems;
  177.   printf("   ----- Name -----     Session     Input     Output     Time          CPS\n");
  178.   printf("                         count                         HH:MM:SS   Min  Avr  Max\n");
  179.   Report_BBS(ptr);
  180.   if( sflag )
  181.     {
  182.     hr  = ptr->time_used[0];
  183.     sec = hr % 60;
  184.     hr /= 60;
  185.     mins = hr %60;
  186.     hr /= 60;
  187.     if( ptr->time_used[0] != 0)
  188.       {
  189.       avr_cps = (ptr->chars_in[0] + ptr->chars_out[0] ) / ptr->time_used[0];
  190.       }
  191.     else avr_cps = 0;
  192.     printf("%23s-------  ---------  ---------   --------\n"," ");
  193.     printf(" %20s  %7ld %10ld %10ld  %3ld:%02ld:%02ld       %4ld\n",
  194.     " ", ptr->net_count[0], ptr->chars_in[0], ptr->chars_out[0], hr, mins, sec
  195.     ,  avr_cps);
  196.     };
  197.   }
  198.  
  199. void Report_BBS(struct bbs *ptr)
  200.   {
  201.   int idx;
  202.   long avr_cps;
  203.   long hr, mins, sec;
  204.   if( ptr->next != NULL) Report_BBS(ptr->next);
  205.   for( idx=0; idx < 12 && mflag; idx++)
  206.     {
  207.     if( ptr->time_used[idx] == 0)continue;
  208.     hr  = ptr->time_used[idx];
  209.     sec = hr % 60;
  210.     hr /= 60;
  211.     mins = hr %60;
  212.     hr /= 60;
  213.     avr_cps = (ptr->chars_in[idx] + ptr->chars_out[idx] ) / ptr->time_used[idx];
  214.     printf(" %20s  %7ld %10ld %10ld  %3ld:%02ld:%02ld  %4ld %4ld %4ld\n",
  215.       month[idx], ptr->net_count[idx], ptr->chars_in[idx], ptr->chars_out[idx], hr, mins, sec,
  216.       ptr->min_cps[idx], avr_cps, ptr->max_cps[idx]);
  217.     if( idx > 0)
  218.       {
  219.       ptr->net_count[0] += ptr->net_count[idx];
  220.       ptr->chars_in[0]  += ptr->chars_in[idx];
  221.       ptr->chars_out[0] += ptr->chars_out[idx];
  222.       ptr->time_used[0] += ptr->time_used[idx];
  223.       if( ptr->min_cps[0] > ptr->min_cps[idx]) ptr->min_cps[0] = ptr->min_cps[idx];
  224.       if( ptr->max_cps[0] < ptr->max_cps[idx]) ptr->max_cps[0] = ptr->max_cps[idx];
  225.       };
  226.     };
  227.  
  228.   hr  = ptr->time_used[0];
  229.   sec = hr % 60;
  230.   hr /= 60;
  231.   mins = hr %60;
  232.   hr /= 60;
  233.   if( ptr->time_used[0] != 0)
  234.     {
  235.     avr_cps = (ptr->chars_in[0] + ptr->chars_out[0] ) / ptr->time_used[0];
  236.     }
  237.   else avr_cps = 0;
  238.   if( mflag  )
  239.     printf("%23s-------  ---------  ---------   --------\n"," ");
  240.   printf(" %20s  %7ld %10ld %10ld  %3ld:%02ld:%02ld  %4ld %4ld %4ld\n",
  241.   ptr->name, ptr->net_count[0], ptr->chars_in[0], ptr->chars_out[0], hr, mins, sec
  242.   , ptr->min_cps[0], avr_cps, ptr->max_cps[0]);
  243.   if( mflag )printf("\n");
  244.   if( ptr->next != 0 && sflag )
  245.       {
  246.       ptr->chars_in[0]  += ptr->next->chars_in[0];
  247.       ptr->chars_out[0] += ptr->next->chars_out[0];
  248.       ptr->time_used[0] += ptr->next->time_used[0];
  249.       ptr->net_count[0] += ptr->next->net_count[0];
  250.       };
  251.  
  252.   }
  253.